﻿using FineUI;
using SysManage.BLL;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ZHDJ.Core.Caching;
using ZHDJ.Core.Util;
using ZHDJ.Data.Models;
using ZHDJ.WebPage;

namespace SysManage.Module.VideoProbe
{
    public partial class VideoProbeEdit : PageBase
    {
        #region ViewPower

        /// <summary>
        /// 本页面的浏览权限，空字符串表示本页面不受权限控制
        /// </summary>
        public override string ViewPower
        {
            get
            {
                return "";
            }
        }

        #endregion

        #region Page_Load

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindData();
                LoadData();
            }
        }

        private void BindData()
        {

        }

        private void LoadData()
        {
            btnClose.OnClientClick = ActiveWindow.GetHideReference();
            string id = GetQueryIntValue("id");
            if (id != "")
            {
                partyvideoprobe article = DB.partyvideoprobes.Find(id);
                if (article == null)
                {
                    // 参数错误，首先弹出Alert对话框然后关闭弹出窗口
                    Alert.Show("参数错误！", String.Empty, ActiveWindow.GetHideReference());
                    return;
                }

                this.ppBranch.Text = ControlBusiness.GetBranchPickerText(article.BranchID);
                this.ppBranch.Value = article.BranchID;
                this.txtCameraName.Text = article.CameraName;

                this.txtAddressTelecom.Text = article.AddressTelecom;
                this.txtPortTelecom.Text = article.PortTelecom;
                this.txtAddressUnicon.Text = article.AddressUnicon;
                this.txtPortUnicon.Text = article.PortUnicon;
                this.txtAddressMobile.Text = article.AddressMobile;
                this.txtPortMobile.Text = article.PortMobile;

                this.txtCameraLoginName.Text = article.CameraLoginName;
                this.txtCameraLoginPwd.Text = article.CameraLoginPwd;
                this.txtDescription.Text = article.Description;
                this.txtLinkMan.Text = article.LinkMan;
                this.txtLinkPhone.Text = article.LinkPhone;
            }
        }
        #endregion

        #region Events

        private void SaveData(bool isPublish)
        {
            string id = GetQueryIntValue("id");

            if (id != "")
            {
                //更新
                partyvideoprobe article = DB.partyvideoprobes.Find(id);
                if (article == null)
                {
                    Alert.Show("参数错误！", String.Empty, ActiveWindow.GetHideReference());
                    return;
                }

                article.BranchID = this.ppBranch.Value;
                article.CameraName = this.txtCameraName.Text;

                article.AddressTelecom = this.txtAddressTelecom.Text;
                article.PortTelecom = this.txtPortTelecom.Text;
                article.AddressUnicon = this.txtAddressUnicon.Text;
                article.PortUnicon = this.txtPortUnicon.Text;
                article.AddressMobile = this.txtAddressMobile.Text;
                article.PortMobile = this.txtPortMobile.Text;
                
                article.CameraLoginName = this.txtCameraLoginName.Text;
                article.CameraLoginPwd = this.txtCameraLoginPwd.Text;
                article.ModifyTime = DateTime.Now;
                article.ModifyBy = CurrentUser.LoginID;
                article.LinkMan = this.txtLinkMan.Text;
                article.LinkPhone = this.txtLinkPhone.Text;
                DB.SaveChanges();
            }
            else
            {
                partyvideoprobe article = new partyvideoprobe();
                article.ID = StringHelper.NewID;
                article.BranchID = this.ppBranch.Value;
                article.CameraName = this.txtCameraName.Text;
                article.AddressTelecom = this.txtAddressTelecom.Text;
                article.PortTelecom = this.txtPortTelecom.Text;
                article.AddressUnicon = this.txtAddressUnicon.Text;
                article.PortUnicon = this.txtPortUnicon.Text;
                article.AddressMobile = this.txtAddressMobile.Text;
                article.PortMobile = this.txtPortMobile.Text;
                article.CameraLoginName = this.txtCameraLoginName.Text;
                article.CameraLoginPwd = this.txtCameraLoginPwd.Text;
                article.CreateTime = DateTime.Now;
                article.CreateBy = CurrentUser.LoginID;
                article.ModifyTime = DateTime.Now;
                article.ModifyBy = CurrentUser.LoginID;
                article.LinkMan = this.txtLinkMan.Text;
                article.LinkPhone = this.txtLinkPhone.Text;
                DB.partyvideoprobes.Add(article);
                DB.SaveChanges();
            }

            MemoryCacheManager CacheManager = new MemoryCacheManager();
            CacheManager.Remove(CacheKeys.Cache_Branch_Camera_Tree_Navigation);
            CacheManager.Remove(CacheKeys.Cache_Camera_Branch_All);
        }

        /// <summary>
        /// 保存并关闭按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnSaveClose_Click(object sender, EventArgs e)
        {
            SaveData(false);

            PageContext.RegisterStartupScript(ActiveWindow.GetHidePostBackReference());
        }
        #endregion
    }
}